--[[
date_translator: 将 `date` 翻译为当前日期

translator 的功能是将分好段的输入串翻译为一系列候选项。

欲定义的 translator 包含三个输入参数：
 - input: 待翻译的字符串
 - seg: 包含 `start` 和 `_end` 两个属性，分别表示当前串在输入框中的起始和结束位置
 - env: 可选参数，表示 translator 所处的环境（本例没有体现）

translator 的输出是若干候选项。
与通常的函数使用 `return` 返回不同，translator 要求您使用 `yield` 产生候选项。

`yield` 每次只能产生一个候选项。有多个候选时，可以多次使用 `yield` 。

请看如下示例：
--]]
require("lunarDate")
require("lunarGz")
require("lunarJq")

function CnDate_translator(y)
	 local t,cstr,t2,t1
	 cstr = {"〇","一","二","三","四","五","六","七","八","九"}  t=""  t1=tostring(y)
	if t1.len(tostring(t1))~=8 then return t1 end
	 for i =1,t1.len(t1) do
		  t2=cstr[tonumber(t1.sub(t1,i,i))+1]
		  if i==5 and t2 ~= "〇" then t2="年十" elseif i==5 and t2 == "〇" then t2="年"  end
		  if i==6 and t2 ~= "〇" then t2 =t2 .. "月" elseif i==6 and t2 == "〇" then t2="月"  end
		  --if t.sub(t,t.len(t)-1)=="年" then t2=t2 .. "月" end
		  if i==7 and tonumber(t1.sub(t1,7,7))>1 then t2= t2 .. "十" elseif i==7 and t2 == "〇" then t2="" elseif i==7 and tonumber(t1.sub(t1,7,7))==1 then t2="十" end
		  if i==8 and t2 ~= "〇" then t2 =t2 .. "日" elseif i==8 and t2 == "〇" then t2="日"  end
		  t=t .. t2
	end
		  return t
end

local GetLunarSichen= function(time,t)
	local time=tonumber(time)
	local LunarSichen = {"子时(夜半｜三更)", "丑时(鸡鸣｜四更)", "寅时(平旦｜五更)", "卯时(日出)", "辰时(食时)", "巳时(隅中)", "午时(日中)", "未时(日昳)", "申时(哺时)", "酉时(日入)", "戌时(黄昏｜一更)", "亥时(人定｜二更)"}
	if tonumber(t)==1 then sj=math.floor((time+1)/2)+1 elseif tonumber(t)==0 then sj=math.floor((time+13)/2)+1 end
	if sj>12 then return LunarSichen[sj-12] else return LunarSichen[sj] end
end


function toNyear(year,mother,day) 
    --天干名称
    local cTianGan = {"甲","乙","丙","丁","戊","己","庚","辛","壬","癸"}
    --地支名称
    local cDiZhi = {"子","丑","寅","卯","辰","巳","午", "未","申","酉","戌","亥"}
    --属相名称
    local cShuXiang = {"鼠","牛","虎","兔","龙","蛇", "马","羊","猴","鸡","狗","猪"}
    --农历日期名
    local cDayName =
    {
        "*","初一","初二","初三","初四","初五",
        "初六","初七","初八","初九","初十",
        "十一","十二","十三","十四","十五",
        "十六","十七","十八","十九","二十",
        "廿一","廿二","廿三","廿四","廿五",
        "廿六","廿七","廿八","廿九","三十"
    }
    --农历月份名
    local cMonName = {"*","正","二","三","四","五","六", "七","八","九","十","十一","腊"}

    --公历每月前面的天数
    local wMonthAdd = {0,31,59,90,120,151,181,212,243,273,304,334}
    -- 农历数据
    local wNongliData = {2635,333387,1701,1748,267701,694,2391,133423,1175,396438
    ,3402,3749,331177,1453,694,201326,2350,465197,3221,3402
    ,400202,2901,1386,267611,605,2349,137515,2709,464533,1738
    ,2901,330421,1242,2651,199255,1323,529706,3733,1706,398762
    ,2741,1206,267438,2647,1318,204070,3477,461653,1386,2413
    ,330077,1197,2637,268877,3365,531109,2900,2922,398042,2395
    ,1179,267415,2635,661067,1701,1748,398772,2742,2391,330031
    ,1175,1611,200010,3749,527717,1452,2742,332397,2350,3222
    ,268949,3402,3493,133973,1386,464219,605,2349,334123,2709
    ,2890,267946,2773,592565,1210,2651,395863,1323,2707,265877
	,1706,2773,133557,1206,398510,2638,3366,335142,3411,1450,200042
	,2413,723293,1197,2637,399947,3365,3410,334676,2906}

    local wCurYear,wCurMonth,wCurDay;
    local nTheDate,nIsEnd,m,k,n,i,nBit;
    local szNongli, szNongliDay,szShuXiang;
    ---取当前公历年、月、日---
    wCurYear = tonumber(year);
    wCurMonth = tonumber(mother);
    wCurDay = tonumber(day);
    ---计算到初始时间1921年2月8日的天数：1921-2-8(正月初一)---
    nTheDate = (wCurYear - 1921) * 365 + (wCurYear - 1921) / 4 + wCurDay + wMonthAdd[wCurMonth] - 38
    if (((wCurYear % 4) == 0) and (wCurMonth > 2)) then
        nTheDate = nTheDate + 1
    end
    
    --计算农历天干、地支、月、日---
    nIsEnd = 0;
    m = 0;
    while nIsEnd ~= 1 do
        if wNongliData[m+1] < 4095 then
            k = 11;
        else
            k = 12;
        end
        n = k;
        while n>=0 do
            --获取wNongliData(m)的第n个二进制位的值
            nBit = wNongliData[m+1];
            for i=1,n do
                nBit = math.floor(nBit/2);
            end
            nBit = nBit % 2;
            if nTheDate <= (29 + nBit) then
                nIsEnd = 1;
                break
            end
            nTheDate = nTheDate - 29 - nBit;
            n = n - 1;
        end
        if nIsEnd ~= 0 then
            break;
        end
        m = m + 1;
    end

    wCurYear = 1921 + m;
    wCurMonth = k - n + 1;
    wCurDay = nTheDate;
    if k == 12 then
        if wCurMonth == wNongliData[m+1] / 65536 + 1 then
            wCurMonth = 1 - wCurMonth;
        elseif wCurMonth > wNongliData[m+1] / 65536 + 1 then
            wCurMonth = wCurMonth - 1;
        end
    end
    wCurDay = math.floor(wCurDay)
    --print('农历', wCurYear, wCurMonth, wCurDay)
    --生成农历天干、地支、属相 ==> wNongli--
    szShuXiang = cShuXiang[(((wCurYear - 4) % 60) % 12) + 1]
    --szShuXiang = cShuXiang[(((wCurYear - 4) % 60) % 12) + 1];
    szNongli = cTianGan[(((wCurYear - 4) % 60) % 10)+1] .. cDiZhi[(((wCurYear - 4) % 60) % 12) + 1] .. '(' .. szShuXiang.. ')' .. '年'
    --szNongli,"%s(%s%s)年",szShuXiang,cTianGan[((wCurYear - 4) % 60) % 10],cDiZhi[((wCurYear - 4) % 60) % 12]);

    --生成农历月、日 ==> wNongliDay--*/
    if wCurMonth < 1 then
        szNongliDay =  "闰" .. cMonName[(-1 * wCurMonth) + 1]
    else
        szNongliDay = cMonName[wCurMonth+1]
    end

    szNongliDay =  szNongliDay .. "月" .. cDayName[wCurDay+1]
    return szNongli .. szNongliDay
end


local function translator(input, seg)
	  --date0 2020年02月04日
      --date0=os.date("%Y年%m月%d日")
	  
	  date_y=os.date("%Y") --取年
	  date_m=os.date("%m") --取月
	  date_d=os.date("%d") --取日

	  num_m=os.date("%m")+0
	  num_m1=math.modf(num_m)
	  num_d=os.date("%d")+0
	  num_d1=math.modf(num_d)
	  
	 --date1 2020-2-4
	 date1=tostring(date_y).."-"..tostring(num_m1).."-"..tostring(num_d1)
	 --date2 2020/2/4
	 date2=tostring(date_y).."/"..tostring(num_m1).."/"..tostring(num_d1)
	 
	  --date_zw1 2020年2月4日
	  date_zw1=os.date("%Y年")..tostring(num_m1).."月"..tostring(num_d1).."日"

   --date_zw2 二〇二〇年十一月二十六日
   date_zw2=CnDate_translator(os.date("%Y%m%d"))
   
    --date_num1 20200204
	date_num1=os.date("%Y%m%d")
	
    --date_num2 20200204
	date_num2=tostring(date_y).."."..tostring(num_m1).."."..tostring(num_d1)
	
    --date_lunar1 农历
	date_lunar1=Date2LunarDate(os.date("%Y%m%d"))
	
	--农历天干地支
	date_gz_y=lunarJzl(os.date("%Y%m%d%H"))[1].."年"
	date_gz_m=lunarJzl(os.date("%Y%m%d%H"))[2].."月"
	date_gz_d=lunarJzl(os.date("%Y%m%d%H"))[3].."日"
	date_gz_t=lunarJzl(os.date("%Y%m%d%H"))[4].."时"
	
	date_gz=date_gz_y.." "..date_gz_m.." "..date_gz_d.." "..date_gz_t
	
	--农历节气
	date_jq = JQtest(os.date("%Y%m%d"))
	
	--time1 06:06:06
	time1=os.date("%H:%M:%S")
	
	--time2 6点6分6秒
	time_zw1=os.date("%H点%M分%S秒")
	

	time_gz=GetLunarSichen(os.date("%H"),1)

	  --星期
	 local cxq = {"星期日","星期一","星期二","星期三","星期四","星期五","星期六"}
	 local exq = {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"}
	 local exqj = {"Sun.","Mon.","Tue.","Wed.","Thu.","Fri.","Sat."}
	 local cxqz = {"周日","周一","周二","周三","周四","周五","周六"}

	 day_w=os.date("%w")
	 day_w1=cxq[math.modf(day_w)+1]
	 day_w2=exq[math.modf(day_w)+1]
	 day_w3=exqj[math.modf(day_w)+1]
	 day_w4=cxqz[math.modf(day_w)+1]
	 
	 
  
   if (input == "date" or input == "iuja" or input == "rejj" or input == "pedl" or input == "/now" or input == "/qrq" or input == "/rq" or input == "/nyr" or input == "/nl"  or input == "/nyr" or input == "/date" or input == "/iuja" or input == "/rejj" or input == "/pedl" or input == "/jjad") then
      yield(Candidate("date", seg.start, seg._end, date1, "〔日期〕"))
	  yield(Candidate("date", seg.start, seg._end, date2, "〔日期〕"))
      yield(Candidate("date", seg.start, seg._end, date_zw1, "〔中文日期〕"))
      yield(Candidate("date", seg.start, seg._end, date_zw2, "〔中文日期〕"))
	  yield(Candidate("date", seg.start, seg._end, date_lunar1, "〔公历⇉农历〕"))
	  yield(Candidate("date", seg.start, seg._end, date_gz, "〔公历⇉干支〕"))
	  if (date_jq ~= nil and date_jq ~="") then
		yield(Candidate("date", seg.start, seg._end, date_jq, "〔公历⇉节气〕"))
	  end
	  yield(Candidate("date", seg.start, seg._end, date_num1, "〔公历⇉数字〕"))
	  yield(Candidate("date", seg.start, seg._end, date_num2, "〔公历⇉数字〕"))
   end

   if (input == "time" or input == "/time" or input == "iuju" or input == "jwti" or input == "/sj" or input == "/sfm" or input == "sfm" or input == "dqsj") then
      yield(Candidate("time", seg.start, seg._end, time1, "〔时间〕"))
      yield(Candidate("time", seg.start, seg._end, time_zw1,"〔时间〕"))
	  yield(Candidate("time", seg.start, seg._end, date_gz_t,"〔时间⇉干支〕"))
	  yield(Candidate("time", seg.start, seg._end, time_gz,"〔时间⇉干支〕"))
	  yield(Candidate("time", seg.start, seg._end, date1 .. " "..time1, "〔日期〕"))
	  yield(Candidate("time", seg.start, seg._end, date_zw1 .. " "..time_zw1, "〔日期〕"))

   end

   if (input == "week" or input == "jamt" or input == "/week" or input == "/xq" or input == "/jtad" or input == "/jaj" or input == "/xqj") then
      yield(Candidate("week", seg.start, seg._end, os.date(day_w1), "〔星期〕"))
	  yield(Candidate("week", seg.start, seg._end, os.date(day_w2), "〔星期〕"))
	  yield(Candidate("week", seg.start, seg._end, date_zw1 .. " "..os.date(day_w1), "〔日期〕"))
	  yield(Candidate("date", seg.start, seg._end, date1 .. " "..os.date(day_w2), "〔日期〕"))
      yield(Candidate("week", seg.start, seg._end, os.date(day_w3), "〔星期〕"))
      yield(Candidate("week", seg.start, seg._end, os.date(day_w4), "〔星期〕"))

   end

   if (input == "now" or input == "/now" or input == "jaju") then
      yield(Candidate("time", seg.start, seg._end, date1 .. " "..time1, "〔当前时间〕"))
	  yield(Candidate("date", seg.start, seg._end, date1, "〔当前日期〕"))
      yield(Candidate("time", seg.start, seg._end, time1, "〔当前时间〕"))
	  yield(Candidate("time", seg.start, seg._end, time_gz,"〔时间⇉干支〕"))
      yield(Candidate("week", seg.start, seg._end, os.date(day_w1), "〔当前星期〕"))
	  yield(Candidate("week", seg.start, seg._end, os.date(day_w2), "〔当前星期〕"))
   end
   
   if (input == "sj") then
      yield(Candidate("time", seg.start, seg._end, date1 .. " "..time1, "〔当前时间〕"))
   end
   
      if (input == "rq") then
	  yield(Candidate("date", seg.start, seg._end, date1, "〔当前日期〕"))
   end
      if (input == "xq") then
	  yield(Candidate("week", seg.start, seg._end, os.date(day_w1), "〔星期〕"))
   end
end


-- 将上述定义导出
return translator
